package com.yxr.mobileplayer.activity;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.yxr.mobileplayer.R;
import com.yxr.mobileplayer.base.BasePage;
import com.yxr.mobileplayer.base.ReplaceFragment;
import com.yxr.mobileplayer.entity.MediaItem;
import com.yxr.mobileplayer.page.NativeAudioPage;
import com.yxr.mobileplayer.page.NativeVideoPage;
import com.yxr.mobileplayer.page.NetAudioPage;
import com.yxr.mobileplayer.page.NetVideoPage;
import com.yxr.mobileplayer.tools.LArrayList;


public class HomeActivity extends FragmentActivity  implements ActivityCompat.OnRequestPermissionsResultCallback{

    private RadioGroup radioGroup;
    private LArrayList<BasePage> pageList;
    private int pageIndex;
    private LArrayList<MediaItem > mediaItems = new LArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        radioGroup = findViewById(R.id.rg_main);


        //初始化所以页面

        pageList = new LArrayList<BasePage>()
                .ladd(0,new NativeVideoPage(this,mediaItems))
                .ladd(1,new NativeAudioPage(this))
                .ladd(2,new NetVideoPage(this))
                .ladd(3,new NetAudioPage(this));
        //设置按钮组 选择改变事情
        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            // 选择页面
            switch (checkedId){
                case R.id.rb_video : pageIndex = 0 ; break;
                case R.id.rb_audio : pageIndex = 1 ; break;
                case R.id.rb_net_video : pageIndex = 2 ; break;
                case R.id.rb_net_audio : pageIndex = 3 ; break;
                default : pageIndex = 0 ;
            }
            //设置页面
            FragmentManager fragmentManager = getSupportFragmentManager(); //获取FragmentManager
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();//开启事务
            fragmentTransaction.replace( R.id.fl_home, new ReplaceFragment(pageList,pageIndex) );
            fragmentTransaction.commit();//事务提交
        });
        //默认选择本地视频
        radioGroup.check(R.id.rb_video); //会自动调用 setOnCheckedChangeListener 方法
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        ContentResolver contentResolver = getContentResolver();
        Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        String[] objects = {
                MediaStore.Video.Media.DISPLAY_NAME,//在Sdcard显示的名称
                MediaStore.Video.Media.DURATION,//视频的长度
                MediaStore.Video.Media.SIZE,//视频文件大小
                MediaStore.Video.Media.DATA//视频的绝对地址
        };
        Cursor cursor =  contentResolver.query(uri, objects, null, null, null);
        if(cursor != null){
            while (cursor.moveToNext()){
                MediaItem mediaItem = new MediaItem();
                String name = cursor.getString(0);
                mediaItem.setName(name);
                long duration = cursor.getLong(1);
                mediaItem.setDuration(duration);
                long size = cursor.getLong(2);
                mediaItem.setSize(size);
                String data = cursor.getString(3);
                mediaItem.setData(data);
                //把视频添加到列表中
                mediaItems.add(mediaItem);
            }
            cursor.close();
            NativeVideoPage nvp = (NativeVideoPage) pageList.get(0);
            nvp.getHandler().sendEmptyMessage(1001);
        }
    }




}
